{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Array Creation Function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# import numpy \n", "import numpy as np " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using `zeros()`\n", "- Returns an array of given shape and type filled with zeros \n", "- **Syntax:** `np.zeros(shape, dtype)`\n", " - shape - integer or sequence of integers\n", " - dtype - data type(default: float)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0.]\n" ] } ], "source": [ "# 1D array of length 3 with all values 0 \n", "Z1 = np.zeros(3)\n", "print(Z1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]\n", " [0. 0. 0. 0.]]\n" ] } ], "source": [ "# 2D array of 3x4 with all values 0 \n", "Z2 = np.zeros((3,4))\n", "print(Z2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using `ones()`\n", "- Returns an array of given shape and type filled with ones \n", "- **Syntax:** `np.ones(shape, dtype)`\n", " - shape - integer or sequence of integers \n", " - dtype - data type(default: float) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 1. 1.]\n" ] } ], "source": [ "# 1D array of length 3 with all values 1\n", "A1 = np.ones(3) \n", "print(A1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Note__\n", "- Rows = 3 \n", "- Columns = 4 " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1. 1.]\n", " [1. 1. 1. 1.]\n", " [1. 1. 1. 1.]]\n" ] } ], "source": [ "# 2D array of 3x4 with all values 1\n", "A2 = np.ones((3,4))\n", "A2\n", "print(A2) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using `arange()`\n", "- Returns equally spaced numbers with in the given range based on step size. \n", "- **Syntax:** `np.arange(start, stop, step)`\n", " - start- starts of interval range \n", " - stop - end of interval range '\n", " - step - step size of interval " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "# not specify start and step \n", "A1 = np.arange(10)\n", "print(A1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 3 5 7 9]\n" ] } ], "source": [ "# specifying start and step \n", "A2 = np.arange(start=1, stop=10, step=2)\n", "print(A2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10 12 14 16 18 20 22 24]\n" ] } ], "source": [ "# another way \n", "A3 = np.arange(10, 25, 2)\n", "print(A3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using `linspace()`\n", "- Returns equally spaced numbers within the given range based on the sample number. \n", "- **Syntax:** `np.linspace(start, stop, num, dtype, retstep)`\n", " - start-start of interval range \n", " - stop-end of the interval range \n", " - num- number of samples to be generated \n", " - dtype-type of output array \n", " - retstep-return the samples, step values " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]\n" ] } ], "source": [ "# array of evenly spaced values 0 to 2, here sample size = 9\n", "L1 = np.linspace(0,2,9)\n", "print(L1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 20. 40. 60. 80. 100.]\n" ] } ], "source": [ "# Array of 6 evenly divided values from 0 to 100\n", "L2 = np.linspace(0, 100, 6)\n", "print(L2) " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 1.08163265 1.16326531 1.24489796 1.32653061 1.40816327\n", " 1.48979592 1.57142857 1.65306122 1.73469388 1.81632653 1.89795918\n", " 1.97959184 2.06122449 2.14285714 2.2244898 2.30612245 2.3877551\n", " 2.46938776 2.55102041 2.63265306 2.71428571 2.79591837 2.87755102\n", " 2.95918367 3.04081633 3.12244898 3.20408163 3.28571429 3.36734694\n", " 3.44897959 3.53061224 3.6122449 3.69387755 3.7755102 3.85714286\n", " 3.93877551 4.02040816 4.10204082 4.18367347 4.26530612 4.34693878\n", " 4.42857143 4.51020408 4.59183673 4.67346939 4.75510204 4.83673469\n", " 4.91836735 5. ]\n" ] } ], "source": [ "# Array of 1 to 5\n", "L3 = np.linspace(start=1, stop=5, endpoint=True, retstep=False)\n", "print(L3) " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([1. , 1.08163265, 1.16326531, 1.24489796, 1.32653061,\n", " 1.40816327, 1.48979592, 1.57142857, 1.65306122, 1.73469388,\n", " 1.81632653, 1.89795918, 1.97959184, 2.06122449, 2.14285714,\n", " 2.2244898 , 2.30612245, 2.3877551 , 2.46938776, 2.55102041,\n", " 2.63265306, 2.71428571, 2.79591837, 2.87755102, 2.95918367,\n", " 3.04081633, 3.12244898, 3.20408163, 3.28571429, 3.36734694,\n", " 3.44897959, 3.53061224, 3.6122449 , 3.69387755, 3.7755102 ,\n", " 3.85714286, 3.93877551, 4.02040816, 4.10204082, 4.18367347,\n", " 4.26530612, 4.34693878, 4.42857143, 4.51020408, 4.59183673,\n", " 4.67346939, 4.75510204, 4.83673469, 4.91836735, 5. ]), 0.08163265306122448)\n" ] } ], "source": [ "# Array of 1 to 5\n", "L4 = np.linspace(start=1, stop=5, endpoint=True, retstep=True)\n", "print(L4) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Specifying Endpoint__\n", "- `endpoint=True`, inlcude 5 \n", "- `endpoint=False`,exclude 5 \n", "\n", "__Specifying Retstep__\n", "- `retstep=False`, doesn't return the step value\n", "- `endpoint=False`, returns the samples as well step value " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using `logspace()`\n", "- Returns equally spaced numbers within the given range based on the log scale. \n", "- **Syntax:** `np.logspace(start, stop, num, endpoint, base, dtype, retstep)`\n", " - start- start of the sequence \n", " - stop- end of the sequence \n", " - num- number of samples to be generated(default: 50) \n", " - dtype- type of output array \n", " - retstep- return the samples, step values \n", " - endpoint - if true, stop is the last sample \n", " - base - base of the log space(default: 10.0) " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.00000000e+01, 1.77827941e+03, 3.16227766e+05, 5.62341325e+07,\n", " 1.00000000e+10])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate an array with 5 samples with base 10.0 \n", "np.logspace(1, 10, num=5, endpoint=True)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2. , 9.51365692, 45.254834 , 215.2694823 ,\n", " 1024. ])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate an array with 5 samples with base 2.0\n", "np.logspace(1, 10, num=5, endpoint=True, base=2.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate constant arrays using `full()` \n", "- Return a new array of given shape and type, filled with `fill_value`. \n", "- **Syntax:** `np.full(shape,fill_value, dtype)`\n", " - shape - Shape of the new array, e.g., ``(2, 3)`` or ``2``.\n", " - fill_value - Fill value(scaler).\n", " - dtype - The desired data-type for the array" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[7 7]\n", " [7 7]]\n" ] } ], "source": [ "# generate 2x2 constant array, constant = 7\n", "C = np.full((2, 2), 7)\n", "print(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating identity matrix using `eye()`\n", "- An array where all elements are equal to zero, except for the `k`-th\n", " diagonal, whose values are equal to one\n", "- **Syntax:** `np.eye(N, M, k, dtype)`\n", " - N : Number of rows(int) in the output\n", " - M : Number of columns in the output. If None, defaults to `N`.\n", " - k : Index of the diagonal: 0 (the default) refers to the main diagonal,\n", " a positive value refers to an upper diagonal, and a negative value\n", " to a lower diagonal\n", " - dtype: Data-type of the returned array." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0.]\n", " [0. 1.]]\n" ] } ], "source": [ "# generate 2x2 identity matrix \n", "I = np.eye(2)\n", "print(I) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate arrays using random.rand() \n", "- Returns an array of given shape filled with random values. \n", "- **Syntax:** `np.random.rand(shape)`\n", " - shape - integer or sequence of integer " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.04702746 0.23584808 0.50431556 0.69413053 0.47153215]\n" ] } ], "source": [ "# create an array with randomly generated 5 values \n", "R = np.random.rand(5)\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.16624233 0.0964212 ]\n", " [0.78198135 0.73523559]]\n" ] } ], "source": [ "# generate 2x2 array of random values \n", "R1 = np.random.random((2, 2))\n", "print(R1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.93811474 0.88457553 0.43868813 0.28863718 0.22392346]\n", " [0.33501581 0.92690003 0.58024197 0.62907141 0.94718802]\n", " [0.88327637 0.12798794 0.69406163 0.35498295 0.15060932]\n", " [0.05267434 0.13422761 0.02840229 0.4848584 0.09832962]]\n" ] } ], "source": [ "# generate 4x5 array of random floats between 0-1\n", "R2 = np.random.rand(4,5)\n", "print(R2)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[67.99860091 88.35769765 89.62212186 98.4857083 86.63764594 75.9827505\n", " 20.45118608]\n", " [28.22579676 21.4423744 26.9836162 3.35148401 31.04386935 49.08501192\n", " 38.00951035]\n", " [56.2136153 95.53104524 0.54238607 41.52657027 37.75343426 3.98613349\n", " 64.3990297 ]\n", " [21.11354187 73.64098023 14.4115901 93.17250961 1.22209729 20.96891832\n", " 66.18212118]\n", " [55.52430087 85.90128627 11.38223572 36.07691226 50.88500847 74.14025678\n", " 50.78594252]\n", " [93.06227609 95.7403062 89.13154064 90.13460337 4.60962152 28.55976216\n", " 39.69617089]]\n" ] } ], "source": [ "# generate 6x7 array of random floats between 0-100\n", "R3 = np.random.rand(6,7)*100\n", "print(R3)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 2 1]\n", " [0 1 1]]\n" ] } ], "source": [ "# generate 2x3 array of random ints between 0-4\n", "R4 = np.random.randint(5, size=(2,3))\n", "print(R4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate empty arrays using `empty()`\n", "- Return a new array of given shape and type, without initializing entries.\n", "- **Syntax:** `np.empty(shape, dtype)`\n", " - shape - integer or tuple of integer\n", " - dtype - data-type\n" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5.73021895e-300 6.91458629e-310]\n" ] } ], "source": [ "# generate an empty array \n", "E1 = np.empty(2) \n", "print(E1)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.16624233 0.0964212 ]\n", " [0.78198135 0.73523559]]\n" ] } ], "source": [ "# 2x2 empty array\n", "E2 = np.empty((2, 2)) \n", "print(E2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrays using specific data type \n", "- float16\n", "- float32 \n", "- int8\n", "\n", "__SEE MORE__\n", "- https://numpy.org/devdocs/user/basics.types.html" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]], dtype=float16)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate an array of floats \n", "D = np.ones((2, 3, 4), dtype=np.float16)\n", "D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n", "- https://numpy.org/\n", "- https://www.edureka.co/blog/python-numpy-tutorial/\n", "- https://github.com/enthought/Numpy-Tutorial-SciPyConf-2019\n", "- [Python Machine Learning Cookbook](https://www.amazon.com/Python-Machine-Learning-Cookbook-Prateek/dp/1786464470)\n", "
\n", "\n", "*This notebook was created by [Jubayer Hossain](https://jhossain.me/) | Copyright © 2020, [Jubayer Hossain](https://jhossain.me/)*" ] } ], "metadata": { "author": "me ", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }